//
//  SSHomeCell.swift
//  SSSina
//
//  Created by ss on 2017/7/4.
//  Copyright © 2017年 ss. All rights reserved.
//

import UIKit
import SnapKit

class SSHomeCell: UITableViewCell {
    
    //记录底部视图的顶部约束 便于卸载和赋值
    var bottomViewTopConstraint:Constraint?
    
    //提供一个模型属性,供外界赋值
    var statusViewModel:SSStatusViewModel?{
        didSet{
            orginalView.statusViewModel = statusViewModel
        
            //卸载约束
            bottomViewTopConstraint?.deactivate()
        
            //有值
            if let _ = statusViewModel?.homeModel?.retweeted_status{
                // 赋值
                retweetView.statusViewModel = statusViewModel
                // bottomView.top = retweetView.bottom
                bottomView.snp.makeConstraints({ (make) in
                    bottomViewTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
                })
                // 显示转发微博
                retweetView.isHidden = false
            }else{
                // 没有转发微博
                // ottomView.top =  originalView.bottom
                bottomView.snp.makeConstraints({ (make) in
                    bottomViewTopConstraint = make.top.equalTo(orginalView.snp.bottom).constraint
                })
                // 隐藏转发微博
                retweetView.isHidden = true
            }
        }
    }
    
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置视图
    private func setupUI(){
        
        //添加控件
        contentView.addSubview(orginalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetView)
        
        //添加约束
        orginalView.snp.makeConstraints { (make) in
            make.left.top.right.equalTo(contentView)
            //这个约束应该是内部控件决定的
//            make.height.equalTo(50)
        }
        retweetView.snp.makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.top.equalTo(orginalView.snp.bottom)
        }
        bottomView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalTo(contentView)
           bottomViewTopConstraint = make.top.equalTo(orginalView.snp.bottom).constraint
            make.height.equalTo(35)
            make.bottom.equalTo(contentView)
        }
    }
    
    //懒加载控件
    //原创微博
    private lazy var orginalView:SSHomeOriginalView = SSHomeOriginalView()
    //转发微博
    private lazy var retweetView:SSHomeRetweetView = SSHomeRetweetView()
    //底部视图
    private lazy var bottomView:SSHomeBottomView = SSHomeBottomView()

}
